﻿/**
 * author:付祥明
 * 
 * Date: 2015-01-28
 * 
 * 扩展的Panel
 *      增加BorderColor属性
 *      修改其样式为圆角
 * */
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 易保全
{
    [ToolboxItem(true)]
    public partial class ExtendedPanel : Panel
    {
        public ExtendedPanel()
        {
            InitializeComponent();
        }       
        /// <summary>
        /// 圆角角度
        /// </summary>
        private int mMatrixRound = 8;
        [Category("圆角角度"), Description("获得或设置控件圆角角度"), DefaultValue(typeof(int), "8")]
        public int MatrixRound
        {
            get { return mMatrixRound; }
            set
            {
                mMatrixRound = value;
                base.Refresh();
            }
        }
        /// <summary>
        /// 边框颜色
        /// </summary>
        private Color _borderColor = Color.FromArgb(0x33, 0x5e, 0x48);
        [Category("外观"), Description("获得或设置控件边框颜色"), DefaultValue(typeof(Color), "#A7A6AA")]
        public Color BorderColor
        {
            get { return _borderColor; }
            set
            {
                _borderColor = value;
                base.Refresh();
            }
        }
        private GraphicsPath CreateRound(Rectangle rect, int radius)
        {
            GraphicsPath roundRect = new GraphicsPath();
            //顶端 
            roundRect.AddLine(rect.Left + radius - 1, rect.Top - 1, rect.Right - radius, rect.Top - 1);
            //右上角 
            roundRect.AddArc(rect.Right - radius, rect.Top - 1, radius, radius, 270, 90);
            //右边 
            roundRect.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius);
            //右下角
            roundRect.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
            //底边 
            roundRect.AddLine(rect.Right - radius, rect.Bottom, rect.Left + radius, rect.Bottom);
            //左下角 
            roundRect.AddArc(rect.Left - 1, rect.Bottom - radius, radius, radius, 90, 90);
            //左边 
            roundRect.AddLine(rect.Left - 1, rect.Top + radius, rect.Left - 1, rect.Bottom - radius);
            //左上角 
            roundRect.AddArc(rect.Left - 1, rect.Top - 1, radius, radius, 180, 90);
            return roundRect;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            int width = base.Width - base.Margin.Left - base.Margin.Right;
            int height = base.Height - base.Margin.Top - base.Margin.Bottom;
            Rectangle rec = new Rectangle(base.Margin.Left, base.Margin.Top, width, height);
            GraphicsPath round = CreateRound(rec, mMatrixRound);
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.FillPath((Brush)(new SolidBrush(BorderColor)), round);
        }

        protected override void OnResize(EventArgs eventargs)
        {
            base.Refresh();
        }
        ///// <summary>
        ///// 获取进程,以便重绘
        ///// </summary>
        ///// <returns></returns>
        //[DllImport("user32.dll")]
        //static extern IntPtr GetWindowDC(IntPtr hWnd);
        //[DllImport("user32.dll")]
        //static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
        ////获取操作系统消息
        //protected override void WndProc(ref Message m)
        //{
        //    base.WndProc(ref m);
        //    //拦截系统消息,获得控件进程以便重绘
        //    if (m.Msg == 0xf || m.Msg == 0x133)
        //    {
        //        IntPtr hDC = GetWindowDC(m.HWnd);
        //        if (hDC.ToInt32() == 0) return;
        //        //边框样式为FixedSingle事自定义边框样式才有效
        //        if (this.BorderStyle == BorderStyle.FixedSingle)
        //        {
        //            //边框为1像素
        //            Pen pen = new Pen(this._borderColor, 1);
        //            Graphics g = Graphics.FromHdc(hDC);
        //            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        //            g.DrawRectangle(pen, 0, 0, this.Width, this.Height - 1);

        //            pen.Dispose();
        //            //返回结果
        //            m.Result = IntPtr.Zero;
        //            //释放资源
        //            ReleaseDC(m.HWnd, hDC);
        //        }
        //    }
        //}
    }
}
